package com.tbsoft.ehr.action.overtime;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tbsoft.ehr.action.BaseAction;
import com.tbsoft.ehr.model.ServiceCustomer;
import com.tbsoft.ehr.model.StaffInfo;
import com.tbsoft.ehr.model.WorkOvertime;
import com.tbsoft.ehr.service.overtime.OvertimeService;
import com.tbsoft.ehr.service.staffile.StaffFileService;

public class OvertimeAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;	
	private static final Log log = LogFactory.getLog(OvertimeAction.class);
	
	//查询用户信息时存储id
	private String userId;
	private StaffInfo staffInfo;
	private ServiceCustomer serviceCustomer;
	private WorkOvertime overtime;
	private List<WorkOvertime> overtimeLst;
	//条件查询
	private String staffName;
	private String overMonth;
	
	private StaffFileService staffFileService;
	private OvertimeService overtimeService;	
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public StaffInfo getStaffInfo() {
		return staffInfo;
	}

	public WorkOvertime getOvertime() {
		return overtime;
	}

	public void setOvertime(WorkOvertime overtime) {
		this.overtime = overtime;
	}

	public String getStaffName() {
		return staffName;
	}

	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	public String getOverMonth() {
		return overMonth;
	}

	public void setOverMonth(String overMonth) {
		this.overMonth = overMonth;
	}

	public void setStaffInfo(StaffInfo staffInfo) {
		this.staffInfo = staffInfo;
	}

	public ServiceCustomer getServiceCustomer() {
		return serviceCustomer;
	}

	public void setServiceCustomer(ServiceCustomer serviceCustomer) {
		this.serviceCustomer = serviceCustomer;
	}

	public List<WorkOvertime> getOvertimeLst() {
		return overtimeLst;
	}

	public void setOvertimeLst(List<WorkOvertime> overtimeLst) {
		this.overtimeLst = overtimeLst;
	}

	public void setStaffFileService(StaffFileService staffFileService) {
		this.staffFileService = staffFileService;
	}

	public void setOvertimeService(OvertimeService overtimeService) {
		this.overtimeService = overtimeService;
	}

	public String queryUser() {
		if(userId!=null&&userId.length()>0){			
			log.info("输入的用户id为："+this.userId);
			staffInfo = this.staffFileService.getStaffFileByUserId(userId);			
		}
		return "queryUser";		
	}

	public String save() {
		if(staffInfo==null){
			return errorReturn("用户档案信息不存在，请检查");
		}
		overtime.setStaff(staffInfo);
		if(serviceCustomer!=null){
			overtime.setServiceCustomer(serviceCustomer);
		}
		if(overtime.getPay()==null){
			overtime.setPay("N");
		}
		WorkOvertime oldOvertime = overtimeService.queryOvertime(overtime);
		if(oldOvertime!=null){
			overtime.setId(oldOvertime.getId());
			overtime.setHours(overtime.getHours().add(oldOvertime.getHours()));
		}
		overtimeService.addOvertime(overtime);
		setRel("overtime_browse");
		return SUCCESS;
	}
	
	public String query() {
		int pageNum = this.getPageNum() > 0 ? this.getPageNum() - 1 : 0;
		int startIndex = pageNum * getNumPerPage();
		Calendar cd= Calendar.getInstance();
		String yearMonth = "";
		if(!overMonth.isEmpty()){
			if(overMonth.length()==1) {
				yearMonth=cd.get(Calendar.YEAR)+"-0"+overMonth;
			} else if(overMonth.length()==2){
				yearMonth =cd.get(Calendar.YEAR)+"-"+overMonth;
			} else {
				yearMonth = overMonth;
			}
		}
		int count = overtimeService.getOvertimeCount(staffName, yearMonth);
		setTotalCount(count);
		setPageNumShown((getNumPerPage()+count)/getNumPerPage());
		overtimeLst = overtimeService.getOvertimeByCondton(staffName, yearMonth, startIndex, getNumPerPage());
		return "browse";
	}
	
	public String edit(){
		if(overtime!=null&&overtime.getId()!=0){
			overtime = overtimeService.getOvertimeById(overtime.getId());
			return "update";
		}
		return "browse";
	}
	
	public String update(){	
		try {	
			if(overtime.getPay()==null){
				overtime.setPay("N");
			}
			overtimeService.addOvertime(overtime);
		} catch(Exception ex){
			log.error("更新加班信息失败：",ex);
			return errorReturn("更新失败："+ex.getMessage());
		}
		setRel("overtime_browse");
		return SUCCESS;
	}
}
